/***********************************************
 This file is part of redia-library-app-ios.
 
 Copyright (c) 2012, 2013 Redia A/S
 
 Redia-library-app-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-library-app-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-library-app-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */


#import "AbstractXMLRPCClient.h"
#import "defines.h"

@implementation AbstractXMLRPCClient

@synthesize lastXMLRPCCallTimestamp;

- (id)init {
    self = [super init];
    if (self) {
        delegatesPendingConnections = [NSMutableDictionary new];
        self.lastXMLRPCCallTimestamp = [NSDate distantPast];
        dataRefreshTimeout = 1*60*60; //default one hour
    }
    return self;
}

- (void)cancelRequest:(NSString*)identifier
{
	if (identifier==nil) {
		return;
	}
	NSLog(@"Canceling request: %@",identifier);
	
	[manager closeConnectionForIdentifier:identifier];
}

- (void)cancelRequests:(NSArray*)identifiers
{
	if (identifiers==nil) {
		return;
	}
    
	for (NSString* s in identifiers) {
		[self cancelRequest:s];
	}
}


- (void)cancelAllRequestsForDelegate:(id <XMLRPCConnectionDelegate>)d
{
    if (d==nil) {
        return;
    }
    NSValue* handle = [NSValue valueWithPointer:(__bridge const void*)d];
    DLog(@"using handle: %@",handle);

    [self cancelRequests:[delegatesPendingConnections objectForKey:handle]];
    [delegatesPendingConnections removeObjectForKey:handle];
}


- (NSString*)addRequestIdentifier:(NSString*)identifier forDelegate:(id <XMLRPCConnectionDelegate>)theDelegate
{
    if (identifier==nil || [identifier length]==0 || theDelegate==nil) {
        return @"";
    }
    NSValue* handle = [NSValue valueWithPointer:(__bridge const void*)theDelegate];
    DLog(@"using handle: %@ for class: %@",handle, [(NSObject*)theDelegate class]);

    NSMutableArray* prev_connections = [delegatesPendingConnections objectForKey:handle];
    if (prev_connections==nil) {
        prev_connections = [[NSMutableArray alloc] initWithObjects:identifier, nil];
        [delegatesPendingConnections setObject:prev_connections
                                        forKey:handle];
    } else {
        [prev_connections addObject:identifier];
    }
    return identifier;
}

- (void)updateLastCallTimestamp 
{
	self.lastXMLRPCCallTimestamp = [NSDate date];
}


- (BOOL)isRefreshNeeded
{
	NSTimeInterval time_since = -[self.lastXMLRPCCallTimestamp timeIntervalSinceNow];
	if (time_since>dataRefreshTimeout) {
		return TRUE;
	}
	return FALSE;
}

@end
